/*
 * Filename:    IntrgityBlackController.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014年10月30日
 */
package com.paic.mhis.hcpms.trust.web.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.agreement.healthcareprovider.util.HttpProcessUtil;
import com.paic.mhis.hcpms.trust.biz.service.IntegrityLevelSetService;
import com.paic.mhis.hcpms.trust.dto.IntegrityIllegalDTO;
import com.paic.mhis.hcpms.trust.dto.IntegrityLevelDTO;
import com.paic.pafa.appclient.annotation.BizServicesProxy;
import com.paic.pafa.exceptions.BusinessException;

/**
 * 
 * 
 * @author EX-WANGKUI001
 * 
 */
@Controller
@RequestMapping("/intrgityLevelSetController/")
public class IntrgityLevelSetController {
	public static Log log = LogFactory.getLog(IntrgityLevelSetController.class);

	@BizServicesProxy(beanName = "integrityLevelSetServiceImpl")
	private IntegrityLevelSetService integrityLevelSetService;

	/**
	 * 加载关注级别数据
	 * 
	 * @param request
	 * @return
	 * @throws BusinessException
	 */
	@RequestMapping("loadLevelSetData.do")
	@ResponseBody
	public Map<String, Object> loadLevelSetData(HttpServletRequest request) throws BusinessException {

		Map<String, Object> parameter = new HashMap<String, Object>();

		int page = new Integer(request.getParameter("page"));
		int pageSize = new Integer(request.getParameter("pageSize"));

		int startNum = (page - 1) * pageSize;
		int endNum = startNum + pageSize;

		parameter.put("startNum", startNum);
		parameter.put("endNum", endNum);

		List<IntegrityLevelDTO> list = this.integrityLevelSetService.loadIntegrityLevelDTO(parameter);
		long count = this.integrityLevelSetService.loadIntegrityLevelDTOCount(parameter);
		if (null != list && list.size() > 0) {
			for (IntegrityLevelDTO dto : list) {
				if (dto.getLevelName().equals("A")) {
					dto.setLevelNameColor("#FF8C69");
				}
				if (dto.getLevelName().equals("B")) {
					dto.setLevelNameColor("#FFAEB9");
				}
				if (dto.getLevelName().equals("C")) {
					dto.setLevelNameColor("#FFBBFF");
				}
				if (dto.getLevelName().equals("D")) {
					dto.setLevelNameColor("#FFD39B");
				}
				if (dto.getLevelName().equals("E")) {
					dto.setLevelNameColor("#FFE1FF");
				}
			}
		}
		Map<String, Object> returnMap = new HashMap<String, Object>();
		returnMap.put("list", list);
		returnMap.put("count", count);
		return returnMap;
	}

	@RequestMapping("saveLevelSetInfo.do")
	@ResponseBody
	public Map<String, Object> saveLevelSetInfo(HttpServletRequest request) throws BusinessException {

		UserDetails user = (UserDetails) request.getSession().getAttribute("user");

		String str = null;
		try {
			str = HttpProcessUtil.readRequestBody(request);
		} catch (UnsupportedEncodingException e) {
			throw new BusinessException("获取参数出错!请联系管理员");
		} catch (IOException e) {
			throw new BusinessException("获取参数出错!请联系管理员");
		}

		JSONObject transferJson = JSON.parseObject(str);
		String levelId = transferJson.get("levelId").toString();

		JSONArray arrayList = transferJson.getJSONArray("list");

		List<IntegrityIllegalDTO> illeaglList = new ArrayList<IntegrityIllegalDTO>();

		if (arrayList != null && arrayList.size() > 0) {

			for (Object value : arrayList) {
				JSONObject currentObject = JSON.parseObject(value.toString());

				IntegrityIllegalDTO vo = JSONObject.toJavaObject(currentObject, IntegrityIllegalDTO.class);

				illeaglList.add(vo);

			}

		}

		this.integrityLevelSetService.processSaveIntegrityLevelDTO(levelId, user, illeaglList);
		Map<String, Object> returnMap = new HashMap<String, Object>();
		return returnMap;
	}

	@RequestMapping("checkLevelSetInfo.do")
	@ResponseBody
	public Map<String, Object> checkLevelSetInfo(HttpServletRequest request) throws BusinessException {

		UserDetails user = (UserDetails) request.getSession().getAttribute("user");

		String str = null;
		try {
			str = HttpProcessUtil.readRequestBody(request);
		} catch (UnsupportedEncodingException e) {
			throw new BusinessException("获取参数出错!请联系管理员");
		} catch (IOException e) {
			throw new BusinessException("获取参数出错!请联系管理员");
		}

		JSONObject transferJson = JSON.parseObject(str);
		String levelId = transferJson.get("levelId").toString();
		String illegalId = transferJson.get("illegalId").toString();

		boolean flag = this.integrityLevelSetService.checkIlleaglInfoExist(levelId, illegalId);

		Map<String, Object> returnMap = new HashMap<String, Object>();
		returnMap.put("flag", flag);
		return returnMap;
	}

}
